package com.lianxi.test;


import com.lianxi.es.Page;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

@ContextConfiguration(locations = { "classpath:springmvc.xml"})
@WebAppConfiguration
public class EsTest2 {

  /*  @Autowired
    private RestHighLevelClient client;


    @Test
    public void test() throws IOException {
        GetRequest getRequest = new GetRequest("test3", "1");
        GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT);
        Map<String, Object> source = documentFields.getSource();
    }*/


    public RestHighLevelClient getClient(){
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(
                AuthScope.ANY,
                new UsernamePasswordCredentials("",""));
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1",9200)).setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)));
        return client;
    }


    public void myRequest(Page page,QueryBuilder queryBuilder) throws IOException {
        SearchRequest request = new SearchRequest("cms_news");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(queryBuilder);
        sourceBuilder.from((page.getPageNumber()-1)*page.getPageSize());
        sourceBuilder.size(page.getPageSize());
        sourceBuilder.sort("id",SortOrder.DESC);
        request.source(sourceBuilder);
        SearchResponse search = getClient().search(request, RequestOptions.DEFAULT);
        System.out.println(Arrays.toString(search.getHits().getHits()));
        System.out.println(search.getHits().getTotalHits());
    }

    @Test
    public void test2() throws IOException {
        //查询所有
//        myRequest(QueryBuilders.matchAllQuery());
        //条件查询
        Page<Object> page = new Page<>();
        page.setPageNumber(1);
        page.setPageSize(4);
//        myRequest(page,QueryBuilders.termQuery("title","河南"));
//        myRequest(page,QueryBuilders.matchAllQuery());
        //题目模糊
//        myRequest(page,QueryBuilders.fuzzyQuery("title","王凯"));
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.filter(QueryBuilders.termQuery("id","247066"));
//        boolQueryBuilder.must(QueryBuilders.matchAllQuery());
//        boolQueryBuilder.filter(QueryBuilders.termQuery("id",1));
//        boolQueryBuilder.filter(QueryBuilders.wildcardQuery("title","王"));
//        boolQueryBuilder.filter(QueryBuilders.termQuery("siteId","45001"));
        myRequest(page,boolQueryBuilder);
    }
    @Test
    public void test3(){

    }
    @Test
    public void test4(){

    }


    //范围查询 查询年龄在10岁 与30岁之间的
    @Test
    public void test6() throws IOException {

            //范围查询
//            myQueay(QueryBuilders.rangeQuery("age").gt(10).lt(30));

            //前缀查询
//            myQueay(QueryBuilders.prefixQuery("name","金"));

            //通配符查询
//            myQueay(QueryBuilders.wildcardQuery("name","齐*"));

            //多个id指定查询
//            myQueay(QueryBuilders.idsQuery().addIds("1").addIds("2"));

            //多字段查询
//            myQueay(QueryBuilders.multiMatchQuery("器","name"));
    }


    //do...while
    public void dowhileTest(){

    }


}
